﻿// <copyright file="DomainAddEditGM.cs" company="Gravity Age Studios">
// Code released under the MIT license (see License.txt).
// </copyright>
// <author>Vicente Cartas Espinel</author>
// <email>vicente.cartas@gmail.com</email>
namespace LightSwitchApplication
{
    using System.Collections.Generic;
    using LightSwitchApplication.UserCode;
    using Microsoft.LightSwitch;
    using Microsoft.LightSwitch.Presentation.Extensions;

    public partial class DomainAddEditGM
    {
        partial void DomainAddEditGM_InitializeDataWorkspace(List<IDataService> saveChangesTo)
        {
            if (this.DomainId == null)
            {
                this.Domain = new Domain();
                this.Domain.Visibility = (int)VisibilityEnum.Public;
                this.DisplayName = "Add Domain";
            }
            else
            {
                this.Domain = this.DomainQuery;
                this.DisplayName = string.Format("Edit {0}", this.Domain.Name);
            }
        }

        #region Add Existing/Edit DomainProvince

        partial void ProvincesAddAndEditNew_Execute()
        {
            this.FindControl("ModalDomainProvince").DisplayName = string.Format("Add existing Province to {0}", this.Domain.Name);
            this.OpenModalWindow("ModalDomainProvince");
        }

        partial void ProvincesEditSelected_CanExecute(ref bool result)
        {
            result = this.Provinces.SelectedItem != null;
        }

        partial void ProvincesEditSelected_Execute()
        {
            this.Application.ShowDomainProvinceEditGM(this.Provinces.SelectedItem.Id);
        }

        partial void SubmitModalDomainProvince_CanExecute(ref bool result)
        {
            result = this.DomainProvincesSortedByFullName.SelectedItem != null;
        }

        partial void SubmitModalDomainProvince_Execute()
        {
            this.CloseModalWindow("ModalDomainProvince");

            this.DomainProvincesSortedByFullName.SelectedItem.Domain = this.Domain;
            this.DataWorkspace.ApplicationData.SaveChanges();

            this.Provinces.Refresh();
        }

        partial void CancelModalDomainProvince_Execute()
        {
            this.CloseModalWindow("ModalDomainProvince");
        }

        #endregion

        #region Add/Edit LawHolding

        partial void LawHoldingsAddAndEditNew_Execute()
        {
            var holding = this.LawHoldings.AddNew();
            holding.Visibility = (int)VisibilityEnum.Public;

            this.FindControl("ModalLawHolding").DisplayName = string.Format("Add new Law Holding for {0}", this.Domain.Name);
            this.OpenModalWindow("ModalLawHolding");
        }

        partial void LawHoldingsEditSelected_CanExecute(ref bool result)
        {
            result = this.LawHoldings.SelectedItem != null;
        }

        partial void LawHoldingsEditSelected_Execute()
        {
            this.FindControl("ModalLawHolding").DisplayName = "Edit Law Holding";
            this.OpenModalWindow("ModalLawHolding");
        }

        partial void SubmitModalLawHolding_Execute()
        {
            this.CloseModalWindow("ModalLawHolding");
        }

        partial void CancelModalLawHolding_Execute()
        {
            this.CloseModalWindow("ModalLawHolding");
            this.LawHoldings.SelectedItem.Details.DiscardChanges();
        }

        #endregion

        #region Add/Edit TempleHolding

        partial void TempleHoldingsAddAndEditNew_Execute()
        {
            var holding = this.TempleHoldings.AddNew();
            holding.Visibility = (int)VisibilityEnum.Public;

            this.FindControl("ModalTempleHolding").DisplayName = string.Format("Add new Temple Holding for {0}", this.Domain.Name);
            this.OpenModalWindow("ModalTempleHolding");
        }

        partial void TempleHoldingsEditSelected_CanExecute(ref bool result)
        {
            result = this.TempleHoldings.SelectedItem != null;
        }

        partial void TempleHoldingsEditSelected_Execute()
        {
            this.FindControl("ModalTempleHolding").DisplayName = "Edit Temple Holding";
            this.OpenModalWindow("ModalTempleHolding");
        }

        partial void SubmitModalTempleHolding_Execute()
        {
            this.CloseModalWindow("ModalTempleHolding");
        }

        partial void CancelModalTempleHolding_Execute()
        {
            this.CloseModalWindow("ModalTempleHolding");
            this.TempleHoldings.SelectedItem.Details.DiscardChanges();
        }

        #endregion

        #region Add/Edit GuildHolding

        partial void GuildHoldingsAddAndEditNew_Execute()
        {
            var holding = this.GuildHoldings.AddNew();
            holding.Visibility = (int)VisibilityEnum.Public;

            this.FindControl("ModalGuildHolding").DisplayName = string.Format("Add new Guild Holding for {0}", this.Domain.Name);
            this.OpenModalWindow("ModalGuildHolding");
        }

        partial void GuildHoldingsEditSelected_CanExecute(ref bool result)
        {
            result = this.GuildHoldings.SelectedItem != null;
        }

        partial void GuildHoldingsEditSelected_Execute()
        {
            this.FindControl("ModalGuildHolding").DisplayName = "Edit Guild Holding";
            this.OpenModalWindow("ModalGuildHolding");
        }

        partial void SubmitModalGuildHolding_Execute()
        {
            this.CloseModalWindow("ModalGuildHolding");
        }

        partial void CancelModalGuildHolding_Execute()
        {
            this.CloseModalWindow("ModalGuildHolding");
            this.GuildHoldings.SelectedItem.Details.DiscardChanges();
        }

        #endregion

        #region Add/Edit SourceHolding

        partial void SourceHoldingsAddAndEditNew_Execute()
        {
            var holding = this.SourceHoldings.AddNew();
            holding.Visibility = (int)VisibilityEnum.Public;

            this.FindControl("ModalSourceHolding").DisplayName = string.Format("Add new Source Holding for {0}", this.Domain.Name);
            this.OpenModalWindow("ModalSourceHolding");
        }

        partial void SourceHoldingsEditSelected_CanExecute(ref bool result)
        {
            result = this.SourceHoldings.SelectedItem != null;
        }

        partial void SourceHoldingsEditSelected_Execute()
        {
            this.FindControl("ModalSourceHolding").DisplayName = "Edit Source Holding";
            this.OpenModalWindow("ModalSourceHolding");
        }

        partial void SubmitModalSourceHolding_Execute()
        {
            this.CloseModalWindow("ModalSourceHolding");
        }

        partial void CancelModalSourceHolding_Execute()
        {
            this.CloseModalWindow("ModalSourceHolding");
            this.SourceHoldings.SelectedItem.Details.DiscardChanges();
        }

        #endregion

        #region Add/Edit TradeRoute

        partial void TradeRoutesOwnedByDomainAddAndEditNew_CanExecute(ref bool result)
        {
            result = this.GuildHoldings.Count > 0;
        }

        partial void TradeRoutesOwnedByDomainAddAndEditNew_Execute()
        {
            var route = this.TradeRoutesOwnedByDomain.AddNew();
            route.Visibility = (int)VisibilityEnum.Public;
            route.Type = (int)TradeRouteTypeEnum.Land;

            this.FindControl("ModalWindowTradeRouteStart").DisplayName = string.Format("Add new Trade Route for {0}", this.Domain.Name);
            this.OpenModalWindow("ModalWindowTradeRouteStart");
        }

        partial void TradeRoutesOwnedByDomainEditSelected_CanExecute(ref bool result)
        {
            result = this.TradeRoutesOwnedByDomain.SelectedItem != null;
        }

        partial void TradeRoutesOwnedByDomainEditSelected_Execute()
        {
            var tr = this.TradeRoutesOwnedByDomain.SelectedItem;
            if (tr.StartPoint.Owner.Id == this.Domain.Id)
            {
                this.FindControl("ModalWindowTradeRouteStart").DisplayName = "Edit trade route";
                this.OpenModalWindow("ModalWindowTradeRouteStart");
            }
            else
            {
                this.FindControl("ModalWindowTradeRouteEnd").DisplayName = "Edit trade route";
                this.OpenModalWindow("ModalWindowTradeRouteEnd");
            }
        }

        partial void SubmitModalWindowTradeRoute_Execute()
        {
            this.CloseModalWindow("ModalWindowTradeRouteStart");
            this.CloseModalWindow("ModalWindowTradeRouteEnd");
        }

        partial void CancelModalWindowTradeRoute_Execute()
        {
            this.CloseModalWindow("ModalWindowTradeRouteStart");
            this.CloseModalWindow("ModalWindowTradeRouteEnd");
            this.TradeRoutesOwnedByDomain.SelectedItem.Details.DiscardChanges();
        }

        #endregion

        #region Add/Edit LeyLine

        partial void LeyLinesOwnedByDomainAddAndEditNew_CanExecute(ref bool result)
        {
            result = this.SourceHoldings.Count > 0;
        }

        partial void LeyLinesOwnedByDomainAddAndEditNew_Execute()
        {
            var line = this.LeyLinesOwnedByDomain.AddNew();
            line.Visibility = (int)VisibilityEnum.Public;

            this.FindControl("ModalWindowLeyLine").DisplayName = string.Format("Add new Ley Line for {0}", this.Domain.Name);
            this.OpenModalWindow("ModalWindowLeyLine");
        }

        partial void LeyLinesOwnedByDomainEditSelected_CanExecute(ref bool result)
        {
            result = this.LeyLinesOwnedByDomain.SelectedItem != null;
        }

        partial void LeyLinesOwnedByDomainEditSelected_Execute()
        {
            this.FindControl("ModalWindowLeyLine").DisplayName = "Edit ley line";
            this.OpenModalWindow("ModalWindowLeyLine");
        }

        partial void SubmitModalWindowLeyLine_Execute()
        {
            this.CloseModalWindow("ModalWindowLeyLine");
        }

        partial void CancelModalWindowLeyLine_Execute()
        {
            this.CloseModalWindow("ModalWindowLeyLine");
            this.LeyLinesOwnedByDomain.SelectedItem.Details.DiscardChanges();
        }

        #endregion

        #region Add/Edit Army

        partial void ArmiesAddAndEditNew_Execute()
        {
            this.Application.ShowArmyAddEditGM(null);
        }

        partial void ArmiesEditSelected_CanExecute(ref bool result)
        {
            result = this.Armies.SelectedItem != null;
        }

        partial void ArmiesEditSelected_Execute()
        {
            this.Application.ShowArmyAddEditGM(this.Armies.SelectedItem.Id);
        }

        #endregion
    }
}